package com.dementors.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dementors.R;
import com.dementors.model.SeaNew;
import com.dementors.utils.TimeUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/12/3 0003.
 */
public class SeaNewAdapter extends RecyclerView.Adapter<SeaNewAdapter.HeadLineViewHolder> implements View.OnClickListener {
    private List<SeaNew.OverSeaBrands> mList;
    private Context mContext;
    private OnRecyclerViewItemClickListener mOnItemClickListener;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (SeaNew.OverSeaBrands) v.getTag());
        }
    }

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view ,SeaNew.OverSeaBrands brandsbean);
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public SeaNewAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
    }

    public void setList(List<SeaNew.OverSeaBrands> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public SeaNewAdapter.HeadLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(mContext).inflate(R.layout.fragment_sea_new_list,parent,false);
        HeadLineViewHolder holder = new HeadLineViewHolder(view);
        view.setOnClickListener(this);
        return holder;

    }

    @Override
    public void onBindViewHolder(SeaNewAdapter.HeadLineViewHolder holder, int position) {
        SeaNew.OverSeaBrands brands = mList.get(position);
        holder.tvCountry.setText(brands.country_name);
        holder.tvPrice.setText(Integer.parseInt(brands.price)/100+"元起");
        holder.tvTitle.setText(brands.title);
        holder.tvTime.setText("剩余" + TimeUtils.getDayCount(Integer.parseInt(brands.gmt_begin), Integer.parseInt(brands.gmt_end)) + "天");
        if (!"".equals(brands.main_img)) {
            Picasso.with(mContext).load(brands.main_img).placeholder(R.drawable.ic_c2c_default_picture_huati).into(holder.iconLarge);
        }
        if (!"".equals(brands.country_icon)) {
            Picasso.with(mContext).load(brands.country_icon).into(holder.iconCountry);
        }
        holder.itemView.setTag(mList.get(position));
    }

    @Override
    public int getItemCount() {
        Log.i("oye","mList.size():"+mList.size());
        return  mList==null?0:mList.size();
    }

    public  static  class HeadLineViewHolder extends RecyclerView.ViewHolder {
        ImageView iconLarge, iconCountry;
        TextView tvCountry, tvPrice, tvTitle, tvTime;


        public HeadLineViewHolder(View view) {
            super(view);
            tvCountry = (TextView) view.findViewById(R.id.tv_country);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            iconLarge = (ImageView) view.findViewById(R.id.iv_large);
            iconCountry = (ImageView) view.findViewById(R.id.iv_country);
        }

//        @Override
//        public void onClick(View v) {
//
//        }

//    private List<SeaNew.OverSeaBrands> mList;
//    private Context mContext;
//
//
//    public SeaNewAdapter(Context context) {
//        this.mContext = context;
//        mList = new ArrayList<>();
//    }
//
//    public void setList(List<SeaNew.OverSeaBrands> list) {
//        Log.i("oye", list.size() + "adapter");
//        if (list != null) {
//            this.mList = list;
//        }
//
//
//    }
//
////    @Override
////    public int getCount() {
////        Log.i("oye", "我运行到这边了1");
////        return mList == null ? 0 : mList.size();
////
////    }
////
////    @Override
////    public Object getItem(int position) {
////        Log.i("oye", "我运行到这边了2");
////        return mList.get(position);
////    }
//
//    @Override
//    public HeadLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.i("oye", "我运行到这边了2");
//        HeadLineViewHolder holder = new HeadLineViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_sea_new_list, parent, false));
//        return holder;
//    }
//
//    @Override
//    public void onBindViewHolder(HeadLineViewHolder holder, int position) {
//        Log.i("oye", "我运行到这边了3");
//        SeaNew.OverSeaBrands brands = mList.get(position);
//        holder.tvCountry.setText(brands.country_name);
//        holder.tvPrice.setText(brands.price);
//        holder.tvTitle.setText(brands.title);
//        holder.tvTime.setText("剩余" + TimeUtils.getDayCount(Integer.parseInt(brands.gmt_end), Integer.parseInt(brands.gmt_begin)) + "天");
//        if (!"".equals(brands.main_img)) {
//            Picasso.with(mContext).load(brands.main_img).placeholder(R.drawable.ic_c2c_default_picture_huati).into(holder.iconLarge);
//        }
//        if (!"".equals(brands.country_icon)) {
//            Picasso.with(mContext).load(brands.country_icon).into(holder.iconCountry);
//        }
//    }
//
//    @Override
//    public int getItemCount() {
//        Log.i("oye", "我运行到这边了1");
//        return mList==null?0:mList.size();
//    }
//
//
//
//
//    //    @Override
////    public View getView(int position, View convertView, ViewGroup parent) {
////        Log.i("oye", "我运行到这边了4");
////        ViewHolder holder = null;
////        if (convertView == null) {
////            convertView = View.inflate(mContext, R.layout.fragment_sea_new_list, null);
////            holder=new ViewHolder();
////            holder.tvCountry = (TextView) convertView.findViewById(R.id.tv_country);
////            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
////            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
////            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
////            holder.iconLarge = (ImageView) convertView.findViewById(R.id.iv_large);
////            holder.iconCountry = (ImageView) convertView.findViewById(R.id.iv_country);
////            convertView.setTag(holder);
////        } else {
////            holder = (ViewHolder) convertView.getTag();
////        }
////        SeaNew.OverSeaBrands brands = mList.get(position);
////        holder.tvCountry.setText(brands.country_name);
////        holder.tvPrice.setText(brands.price);
////        holder.tvTitle.setText(brands.title);
////        holder.tvTime.setText("剩余" + TimeUtils.getDayCount(Integer.parseInt(brands.gmt_end), Integer.parseInt(brands.gmt_begin)) + "天");
////        if (!"".equals(brands.main_img)) {
////            Picasso.with(mContext).load(brands.main_img).placeholder(R.drawable.ic_c2c_default_picture_huati).into(holder.iconLarge);
////        }
////        if (!"".equals(brands.country_icon)) {
////            Picasso.with(mContext).load(brands.country_icon).into(holder.iconCountry);
////        }
////        return convertView;
////    }
////
////    class ViewHolder {
////        ImageView iconLarge, iconCountry;
////        TextView tvCountry, tvPrice, tvTitle, tvTime;
////    }
//    class HeadLineViewHolder extends RecyclerView.ViewHolder {
//        ImageView iconLarge, iconCountry;
//        TextView tvCountry, tvPrice, tvTitle, tvTime;
//
//        public HeadLineViewHolder(View itemView) {
//            super(itemView);
//            tvCountry = (TextView) itemView.findViewById(R.id.tv_country);
//            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
//            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
//            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
//            iconLarge = (ImageView) itemView.findViewById(R.id.iv_large);
//            iconCountry = (ImageView) itemView.findViewById(R.id.iv_country);
//        }
//    }
    }
}